<?php

  class BPlugin
  {

    /**
     * 跳转
     *
     * @access public
     * @param string $url
     * @param bool $script
     * @param string $msg
     * @return null
     */
    public function redirect($url, $msg = null, $script = false) {
        if (!$script) {
            header('Location: ' . $url);
            exit;
        }

        $language = B::i()->language;
        $package = $language->language('cn')->get('default');
        isset($package[$msg]) && $msg = $package[$msg];
        $str = '<script type="text/javascript">';
        is_null($msg) || $str.= 'alert("' . $msg . '");';
        $str.= 'location.href="' . $url . '";</script>';
        echo $str;
        exit;
    }

    /**
     * 返回消息
     *
     * @access public
     * @param string $word
     */
    function message($message, $url, $time, $values = null)
    {
		$view = B::i()->view;
		$message_ = $message;
        $package = include BLoader::PathName("../language/default/cn");
		if (isset($package[$message]) && $message_ = $package[$message]) {
            if (is_array($values)) {
                foreach ($values as $k => $v) {
                    $rk = $k + 1;
                    $message_ = str_replace('\\'. $rk, $v, $message_);
                }
            } elseif ($values) {
                $message_ = str_replace('\\1', $values, $message_);
            }
        }
		$view->set('message', $message_)->set('url', $url)->set('time', $time);
		return $view->display('message', null, true);
    }

    /**
     * 返回第一个不为空的值
     *
     * @access public
     * @param string $word
     */
	function select()
	{
		if (!$n = func_num_args()){
			return null;
		}
		$i = 0;
		do {
			$arg = func_get_arg($i++);
			if (!is_null($arg) && $arg){
				return $arg;
			}
		} while ($i<$n);
		return null;
	}

	/**
     * 将字符串进行加密
     *
     * @access public
     * @param string $word
     */

	function encrypt($string)
	{
		return md5($string);
	}

    /**
     * 转换编码
     *
     * @access public
     * @param mixed  $content 要转换的内容
     * @param string $from 来源编码
     * @param string $to 转到编码
     * @return mixed
     */
    function transcode($content, $to = 'UTF-8', $from = 'UTF-8,ASCII,GBK,BIG-5') {
        if (is_array($content)) {
            while (list($k, $v) = each($content)) {
                $content[$k] = self::transcode($v, $from, $to);
            }
            return $content;
        }
        return $content ? mb_convert_encoding($content, $to, $from) : null;
    }

    /**
    * 截字
    *
    * @access public
    * @static
    * @param string $string
    * @param int $length
    * @param string $etc
    * @return string
    */
    static function Truncate($content, $length, $etc = '...')
    {
      if ($length == 0) {
          return '';
      } elseif (mb_strlen($content) > $length) {
          $length -= min($length, mb_strlen($etc));
          $charset = 'utf-8';
          $content = mb_substr($content, 0, $length, $charset) . $etc;
      }
      return $content;
    }

      /**
       * 设置CLI模式下程序差异
       *
       * @param unknown_type $name
       * @return unknown
       */
      public function setServerCli($cli = false)
      {
          if ($cli) {
              $args = $_SERVER['argv'];
              $data = array(
                  'exec' => '',
                  'options' => array(),
              );
              $key_server_prefix = 'SERVER.';
              $data['exec'] = array_shift($args);
              while (!is_null($arg = array_shift($args))) {
                  if (substr($arg, 0, 2) === '--') {//option
                      $option = substr($arg, 2);
                      if (false !== strpos($option, '=')) {//--option=argument
                          list($k, $v) = explode('=', $option, 2);
                          if (false !== ($pos = strpos($k, $key_server_prefix))) {
                              $_SERVER[substr($k, $pos + strlen($key_server_prefix))] = $v;
                          } else {
                              $data['options'][$k] = $v;
                          }
                      } else {
                          $data['options'][$option] = 1;
                      }
                      continue;
                  }
              }
              $this->plugin->request->add($data);
          }
      }

      function __get($name)
      {
          $instance = BLoader::instance("../plugin_$name");
          return $instance ? $instance : null;
      }


  }

?>
